# 使用 with 语句打开文件
with open('example.txt', 'w', encoding='utf-8') as file:
    file.write('Hello, World!\n')
    file.write('This is a test file.')

# 文件会在 with 语句块结束后自动关闭
# 无需手动调用 file.close()

# 再次读取文件
with open('example.txt', 'r', encoding='utf-8') as file:
    content = file.read()
    print(content)  # 输出: Hello, World!
                    #       This is a test file.